﻿Public Class AppComponentExplorer
    Inherits ExplorerTreeList

    ' Private aPL As SystemBoxSDK.PL.MetaSDK.MetaControlProps_PL
    'Private pl As SystemBoxSDK.PL.MetaSDK.MetaControlProps_PL = New SystemBoxSDK.PL.MetaSDK.MetaControlProps_PL("SecurityControlProps")
    Private aPl As SystemBoxSDK.PL.MetaSDK.MetaControls_PL

    Public Sub New(ByVal aQueryName As String)
        MyBase.New(aQueryName)
    End Sub

    Public Sub New()
        MyBase.New()
    End Sub

    Public Overrides Sub PopulateDetailSection()
        Me.Cursor = Cursors.WaitCursor
        '
        If aGridControl IsNot Nothing Then
            aGridControl.Dispose()
            aGridControl = Nothing
        End If


        Dim frmCaption As String = NodeDataRow("ControlName").ToString
        Dim guidEnfant As Guid = DirectCast(NodeDataRow(Me.KeyFieldName), Guid)
        Dim aMQ = MetaQuery.GetMetaQueryByName(NodeDataRow("MetaQuerie").ToString)

        aPL = SystemBoxSDK.PL.PLBase.CreateInstanceOf(NodeDataRow("PL").ToString(), aMQ.QueryName)
        'aPL = New PL.MetaSDK.MetaControls_PL(aMQ.QueryName)

        If Not guidEnfant.Equals(Guid.Empty) Then
            aPL.SQLWhere = String.Format("ControlID = '{0}'", guidEnfant)
            'aPl.ControlID_FK = guidEnfant
        End If


        aGridControl = New GridExtPL() With {.Dock = DockStyle.Fill, .Pl = aPL, .SecureGrid = True, .Name = "SecurityMenuForm", .FormCtrlCD = MyBase.PanelName}
        AddHandler aGridControl.InitNewMasterRow, AddressOf MetaControl_NewMasterRow

        Dim action As ActionOption
        action = New ActionOption("SubjectMenu0", "ACT_00187", "Export Subject Security to " + AppManager.ExportToMasterDB, AddressOf ExportToMaster, True, True)
        aGridControl.GridActionOptionList.Add(action)
        action = New ActionOption("SubjectMenu", "ACT_00153", "Export Subject Security", AddressOf Export, True, True)
        aGridControl.GridActionOptionList.Add(action)
        action = New ActionOption("SubjectMenu", "ACT_00154", "Import Subject Security", AddressOf Import, True, True)
        aGridControl.GridActionOptionList.Add(action)


        '
        'On trouve la form pour effacer les controle dedans et ensuite remettre le bon.
        'Comme ca on change le Detail selon la ligne dans L'explorer
        For Each aFrm As XtraForm In Me.ParentForm.MdiChildren
            If aFrm.Name = PanelName Then
                aFrm.Controls.Clear()
                aFrm.Controls.Add(aGridControl)
                aFrm.Text = frmCaption
                aFrm.Show()
                aFrm.Focus()
            End If
            aFrm.Refresh()
        Next

        Me.Cursor = Cursors.Default
    End Sub


    Private Sub MetaControl_NewMasterRow(ByVal sender As Object, ByVal e As InitNewRowEventArgs)
        Dim view As GridView = CType(sender, GridView)
        Dim row As DataRow = view.GetFocusedDataRow()
        aPl.NewRecord(row)
    End Sub

    Private Sub Export(ByVal sender As System.Object, ByVal e As EventArgs)
        Dim aDataRow As DataRow = CType(CType(sender, DXMenuItem).Tag, DataRow)
        Dim propID As Guid
        Using aFile As New System.Windows.Forms.SaveFileDialog
            aFile.DefaultExt = "xml"
            aFile.FileName = "MetaControlProps_" + aDataRow("ControlPropID").ToString
            If aFile.ShowDialog() = DialogResult.OK Then
                propID = New Guid(aDataRow("ControlPropID").ToString)
                If SystemBoxSDK.PL.MetaSDK.MetaControlProps_PL.ExportMetaControlProps(aFile.FileName, propID) Then
                    MessageWindow.ShowMessage("Export Subject Security successful", MsgBoxStyle.Information)
                End If
            End If
        End Using
    End Sub

    Private Sub ExportToMaster(ByVal sender As System.Object, ByVal e As EventArgs)
        Dim isValid As Boolean = False
        Dim listOfDataRows As List(Of DataRow) = CType(CType(sender, DXMenuItem).Tag, List(Of DataRow))

        For Each aDataRow As DataRow In listOfDataRows
            Dim propID As Guid = New Guid(aDataRow("ControlPropID").ToString)
            Try
                isValid = SystemBoxSDK.PL.MetaSDK.MetaControlProps_PL.ExportMetaControlPropsToMaster(propID)
            Catch ex As Exception
                Throw ex
            End Try
        Next
        If isValid Then
            MessageWindow.ShowMessage("Export Subject Security successful", MsgBoxStyle.Information)
        End If
    End Sub

    Private Sub Import(ByVal sender As System.Object, ByVal e As EventArgs)
        Dim saved As Boolean
        Using aFile As New System.Windows.Forms.OpenFileDialog
            aFile.FileName = "*.xml"
            aFile.DefaultExt = "xml"
            aFile.Multiselect = True
            If aFile.ShowDialog() = DialogResult.OK Then
                For Each file As String In aFile.FileNames
                    saved = SystemBoxSDK.PL.MetaSDK.MetaControlProps_PL.ImportMetaControlProps(file)
                Next
                If saved Then
                    MessageWindow.ShowMessage("Import Subject Security successful", MsgBoxStyle.Information)
                End If
            End If
        End Using
    End Sub
End Class
